<?php
/**
 * +----------------------------------------------------------------------
 * | [ DO WHAT YOU LOVE AND FUCK THE REST ]
 * +----------------------------------------------------------------------
 * | EMAIL: < 931274989@qq.com >
 * +----------------------------------------------------------------------
 * | WECHAT: xzc016
 * +----------------------------------------------------------------------
 * | 管理员
 * +----------------------------------------------------------------------
 */
namespace app\admin\controller;

use app\admin\model\Admin as AdminModel;
use app\admin\validate\AdminNickname;
use app\admin\validate\AdminPassword;

use think\facade\View;
use think\facade\Session;
use think\facade\Db;

class Admin extends Base
{
  /**
   * 管理员个人资料修改
   */
  public function personal() 
  {
    //获取管理员id
    $id = Session::get('admin.id',0);
    $model = new AdminModel();
    if($id > 0) {
      //是修改操作
      if($this->request->isPost()) {
        //是提交操作
        $post = $this->request->post();
        //验证
        (new AdminNickname())->goCheck();
        if(false == $model->where('id',$id)->save($post)) {
          return error('修改失败');
        } else {
          adminLog();//写入日志
          return success('修改个人信息成功','admin/admin/personal');
        }
      } else {
        //非提交操作
        $info['admin'] = $model->where('id',$id)->find();
        View::assign('info',$info);
        return View::fetch();
      }
    } else {
      return error('id不正确');
    }
  }

  /**
   * 修改密码
   */
  public function editPassword()
  {
    $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    if($id > 0) {
      if($id == Session::get('admin.id')) {
        $post = $this->request->post();
    		//验证  
	      (new AdminPassword())->goCheck();
    		$admin = Db::name('admin')->where('id',$id)->find();
    		if(password($post['password_old']) == $admin['password']) {
          $password = Db::name('admin')->where('id',$id)->update(['password'=>password($post['password'])]);
          if( !$password ) {
            return error('修改失败');
          } else {
            adminLog();//写入日志
            return success('修改成功','admin/editPassword');
          }
    		} else {
    			return error('原密码错误');
    		}
    	} else {
    		return error('不能修改别人的密码');
    	}
    } else {
      View::assign('id',Session::get('admin.id'));
      return View::fetch();
    }
  }



}

?>